package com.regis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.regis.comment.uilts.ThreadDataUtil;
import com.regis.entity.db.ShoppingCart;
import com.regis.entity.vo.ResultVo;
import com.regis.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 计算机系 ITAEM 摆渡人
 * @description 购物车模块
 * @date 2022/8/23 18:40
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private CacheManager cacheManager;

    /**
     * 添加购物车
     * @param shoppingCart ShoppingCart
     * @return
     */
    @CacheEvict(value = "shoppingCart",key = "#shoppingCart.userId")
    @PostMapping("/add")
    public ResultVo<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id
        shoppingCart.setUserId(ThreadDataUtil.getValue());
        //查询菜品或套餐是否存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId())
                .eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if(one!=null){
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        } else {
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }

        return ResultVo.success(one);
    }

    /**
     * 获取购物车数据
     * @return ResultVo<ShoppingCart>
     */
    @Cacheable(value = "shoppingCart",key = "#threadDataUtil.value",unless = "#result.data == null")
    @GetMapping("/list")
    public ResultVo<List<ShoppingCart>> list(ThreadDataUtil threadDataUtil){
        List<ShoppingCart> list =
                shoppingCartService.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId,
                        ThreadDataUtil.getValue()).orderByAsc(ShoppingCart::getCreateTime));
        return ResultVo.success(list);
    }

    /**
     * 清空购物车
     * @return ResultVo<String>
     */
    @CacheEvict(value = "shoppingCart",key = "#threadDataUtil.value")
    @DeleteMapping("/clean")
    public ResultVo<String> clean(ThreadDataUtil threadDataUtil){
        shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId,
                ThreadDataUtil.getValue()));
        return ResultVo.success("清空成功");
    }

    /**
     * 减少购物车
     * @param shoppingCart ShoppingCart
     * @return ResultVo<ShoppingCart>
     */
    @CacheEvict(value = "shoppingCart",key = "#threadDataUtil.value")
    @PostMapping("/sub")
    public ResultVo<ShoppingCart> delete(@RequestBody ShoppingCart shoppingCart,ThreadDataUtil threadDataUtil){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,ThreadDataUtil.getValue())
                .eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (one != null){
            if(one.getNumber()>1){
                one.setNumber(one.getNumber()-1);
                shoppingCartService.updateById(one);
            }else {
                shoppingCartService.removeById(one);
            }
        }

        return ResultVo.success(one);
    }
}
